package com.base.myproject.client.HR.bcode;

import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.ColumnInfo;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.ui.celleditor.FieldValue;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TriggerField;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;

/*
 * 这个类的作用跟BcodeSelectDialog类似，因为不要选择人员，所以重新做了一个选择部门的弹出框。
 * mzba
 */

public class DepartmentSelectDialog<D extends ModelData> extends TriggerField<D> implements
		Listener<ComponentEvent>, FieldValue {

	private final Window w = new Window();
	private Button okbutton = new Button("确定");
	private Button closebutton = new Button("关闭");
	
	private TreePanel<TreeNodeModelData> tree;
//	private DateTimeFormat activeFormat = DateTimeFormat.getFormat("yyyy-MM-dd H:mm:ss");

//	protected BaseModelStringProvider<TreeNodeModelData> defaultStringProvider = new BaseModelStringProvider<TreeNodeModelData>();

	boolean closeon_ok = true;
	ToolBar tb = new ToolBar();
	boolean neesreselect = true; // 弹开对话框不保存上次选择的数据。

	boolean needfilter = false;
	
	BaseStore<BusiObject> target_store;
	ColumnInfo[] target_col;
	ColumnInfo[] sourcecol;
	
	public DepartmentSelectDialog(){
		init();
	}
	
	public void init(){
		closebutton.addListener(Events.Select, this);
		okbutton.addListener(Events.Select, this);
		initWidget();
		setFireChangeEventOnSetValue(true);
	}
	
	private void initWidget(){
		w.setHeading("选择");
		w.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.people()));
		w.setModal(true);
		w.setSize(434, 480);
		w.setMaximizable(true);
		w.setScrollMode(Scroll.AUTO);
		initTreePanel();
		w.add(inittoolbar());
		w.add(tree);
	}
	
	private Widget inittoolbar() {
		tb.add(okbutton);
		tb.add(closebutton);
		return tb;
	}
	
	public void dopost(final ModelData modeldata) {
		if (sourcecol != null)
			for (int i = 0; i < sourcecol.length; i++) {

				Object o = modeldata.get(sourcecol[i].getName());
				switch (target_col[i].getType()) {
				case ColumnInfo.DOUBLE:
					target_store.setValue(target_col[i].getName(), Double
							.parseDouble(o == null ? "0" : o.toString()));
					break;
				case ColumnInfo.STRING:
					target_store.setValue(target_col[i].getName(), modeldata
							.get(sourcecol[i].getName()));
					break;
				case ColumnInfo.DATETIME:
					target_store.setValue(target_col[i].getName(), modeldata
							.get(sourcecol[i].getName()));
					break;
				default:
					target_store.setValue(target_col[i].getName(), modeldata
							.get(sourcecol[i].getName()));
					break;
				}

				// target_store.setValue(target_col[i],
				// modeldata.get(sourcecol[i]));
			}

		if(isCloseon_ok()){
			w.hide();
		}
	}

	/**
	 * 提交后执行,子类继承
	 * 
	 * @param modeldata
	 */
	public void doposted(List<TreeNodeModelData> modeldatalist) {

	}
	
	public void showEditor() {
		// 判断是否保持上次选择
		if (isNeesreselect()) {
			deselectall();
		}
		w.show();
	}
	
	public void deselectall(){
		tree.getSelectionModel().deselectAll();
		for (TreeNodeModelData m : tree.getCheckedSelection()) {
			tree.setChecked(m, false);
		}
	}

	public void load(){
		String sql = "select id as systemid,name from systeminfo order by id";
		depStore.getStoreinfo().setSql(sql);
		depStore.getStoreinfo().setForcenolimit("true");
		depStore.load();
	}
	
	private BaseTreeStore<TreeNodeModelData> depStore = null;

	private BaseTreeStore<TreeNodeModelData> getDepStore() {
		if (depStore == null) {
			createDepStore();
		}
		return depStore;
	}

	private BaseTreeStore<TreeNodeModelData> createDepStore() {
		StoreInfo si = new StoreInfo();
		si.setTreesortcolumn("systemid");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("Systeminfo");

		depStore = new BaseTreeStore<TreeNodeModelData>(si, false);
		load();
		return depStore;
	}

	private void initTreePanel() {
		tree = new TreePanel<TreeNodeModelData>(getDepStore());
		tree.setDisplayProperty("name");
		tree.setCheckable(true);
		tree.setAutoLoad(true);
		tree.setHeight(400);
		
//		tree.setLabelProvider(defaultStringProvider);
		tree.getStyle().setNodeOpenIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.tree()));
		tree.getStyle().setNodeCloseIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.tree()));
		tree.getStyle().setLeafIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.tree()));
		
		setCheckNodes(CheckNodes.BOTH); // 默认只能选择叶子节点
		tree.setCheckStyle(CheckCascade.NONE); //
		TreePanelSelectionModel<TreeNodeModelData> tpsm = new TreePanelSelectionModel<TreeNodeModelData>();
		tpsm.setSelectionMode(SelectionMode.SINGLE);
		tree.setSelectionModel(tpsm);
	}

	/**
	 * 设置选择的类型 CheckNodes.LEAF CheckNodes.PARENT CheckNodes.BOTH
	 * 
	 * @param checkNodes
	 */
	public void setCheckNodes(CheckNodes checkNodes) {
		tree.setCheckNodes(checkNodes);
	}
	
	public void setExpanded(){
		depStore.addBaseStoreListener(BaseStoreEvent.EventType.LOAD_COMPLETE, new BaseStoreListener(){

			@Override
			public void BaseStoreEventHandle(BaseStoreEvent evt) {
				if(tree.getStore().getModels().size()>0)
					tree.setExpanded(tree.getStore().getModels().get(0), true);
			}

			@Override
			public void onBrowserEvent(Event event) {
				
			}		
		});	
	}
	public TreeNodeModelData findModel(Object value) {
		// 如果本身就是TreeNodeModelData 那么直接返回。
		if (value instanceof TreeNodeModelData) {
			return (TreeNodeModelData) value;
		}
		TreeNodeModelData val = null;
		if (depStore.getModels().size() > 0) {
			for (TreeNodeModelData c : depStore.getAllItems()) {
				if (c.get("value") == null) {
					continue;
				}
				if (c.get("value").equals(value)) {
					val = c;
					break;
				}
			}
		} else {
			val = new TreeNodeModelData();
			val.setNotFlag("value", value);
		}
		return val;
	}
	
	@Override
	protected void onTriggerClick(ComponentEvent ce) {
		super.onTriggerClick(ce);
		showEditor();
	}

	@Override
	public Object getCode() {
		return depStore.getValue("systemid");
	}

	@Override
	public Object getDisplayname() {
		return depStore.getValue("name");
	}

	@Override
	public void setCode(Object code) {

	}

	@Override
	public void setDisplayname(Object displayname) {

	}

	@Override
	public void handleEvent(ComponentEvent be) {
		if (be.getSource().equals(closebutton)) {
			w.hide();
		} else if (be.getSource().equals(okbutton)) {
			dopost(tree.getCheckedSelection().get(0));
			doposted(tree.getCheckedSelection());

		}
	}
	
	public Button getOkbutton() {
		return okbutton;
	}

	public void setOkbutton(Button okbutton) {
		this.okbutton = okbutton;
	}

	public BaseStore<BusiObject> getTarget_store() {
		return target_store;
	}

	public void setTarget_store(BaseStore<BusiObject> targetStore) {
		target_store = targetStore;
	}

	public ColumnInfo[] getTarget_col() {
		return target_col;
	}

	public void setTarget_col(ColumnInfo[] targetCol) {
		target_col = targetCol;
	}

	public ColumnInfo[] getSourcecol() {
		return sourcecol;
	}

	public void setSourcecol(ColumnInfo[] sourcecol) {
		this.sourcecol = sourcecol;
	}
	
	public TreePanel<TreeNodeModelData> getTree() {
		return tree;
	}

	public void setTree(TreePanel<TreeNodeModelData> tree) {
		this.tree = tree;
	}

	public boolean isNeesreselect() {
		return neesreselect;
	}

	/**
	 * true为不保持上次选择的数据，默认为true，否则保持上一次的选择
	 * 
	 * @param neesreselect
	 */

	public void setNeesreselect(boolean neesreselect) {
		this.neesreselect = neesreselect;
	}

	public boolean isCloseon_ok() {
		return closeon_ok;
	}
	/**
	 * 默认点击确定就关闭对话框，默认是true，否则不关闭
	 * @param closeonOk
	 */

	public void setCloseon_ok(boolean closeonOk) {
		this.closeon_ok = closeonOk;
	}

}
